package com.feihao.zhou.service.impl;

import com.feihao.zhou.DBUtils.DaoFactory;
import com.feihao.zhou.DBUtils.TransactionUtil;
import com.feihao.zhou.bean.Employee;
import com.feihao.zhou.dao.EmployeeDao;
import com.feihao.zhou.service.EmployeeService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class EmployeeServiceImpl implements EmployeeService {
    @Override
    public void addEmployee(Employee employee) {
        SqlSession sqlSession = null;
        try {
            sqlSession = DaoFactory.getSqlSession();
            EmployeeDao mapper = DaoFactory.getMapper(sqlSession, EmployeeDao.class);
            mapper.addEmployee(employee);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<Employee> findAll() {
        List<Employee> all = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = DaoFactory.getSqlSession();
            EmployeeDao mapper = DaoFactory.getMapper(sqlSession, EmployeeDao.class);
            all = mapper.findAll();
            return all;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
        return null;
    }

    @Override
    public Employee findById(String id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = DaoFactory.getSqlSession();
            EmployeeDao mapper = DaoFactory.getMapper(sqlSession, EmployeeDao.class);
            Employee byId = mapper.findById(id);
            return byId;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionUtil.rollback(sqlSession);
        } finally {
            TransactionUtil.close(sqlSession);
        }
        return null;
    }

    @Override
    public void updateEmployee(Employee employee) {
        SqlSession sqlSession = null;
        try {
            sqlSession = DaoFactory.getSqlSession();
            EmployeeDao mapper = DaoFactory.getMapper(sqlSession, EmployeeDao.class);
            mapper.updateEmployee(employee);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionUtil.rollback(sqlSession);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void deleteEmployee(String id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = DaoFactory.getSqlSession();
            EmployeeDao mapper = DaoFactory.getMapper(sqlSession, EmployeeDao.class);
            mapper.deleteEmployee(id);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionUtil.rollback(sqlSession);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }
}
